import Vue from 'vue'
import Login from './components/Login.vue'
import Home from './components/Teach/Home.vue'
import Chang from './components/Demon/Chang.vue'
import PersonalMain from './components/Teach/PersonalMain.vue'
import Addcourse from './components/Teach/personalRouter/AddCourse.vue'
import Hom02 from './components/Demon/Home02.vue'
import PernalMessage from './components/Teach/personalRouter/PernalMessage.vue'
import Numberselect from './components/Teach/personalRouter/Numberselect.vue'
import AddCourse2 from './components/Teach/personalRouter/AddCourse2.vue'
import ClassSelect from './components/Teach/personalRouter/ClassSelect.vue'
import Manage from './components/Manage/Manage.vue'
import ClassRoom from './components/Manage/ClassRoom.vue'
import ManageMain from './components/Manage/ManageMain.vue'
import Upload from './components/Manage/upload2.vue'
import ManageSelect from './components/Manage/ManageSelect.vue'
import ChangTeach from './components/Manage/ChangTeach.vue'
import UserManage from './components/Manage/UserManage.vue'
import CourseManage from './components/Manage/CourseManage.vue'
import FreeClassroom from './components/Manage/FreeClassroom.vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home02',
    component: Hom02
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/manage',
    component: Manage,
    children: [
      {
        path: '/classroom',
        component: ClassRoom
      },
      {
        path: '/managemain',
        component: ManageMain
      },
      {
        path: '/upload',
        component: Upload
      },
      {
        path: '/manageselect',
        component: ManageSelect
      },
      {
        path: '/changteach',
        component: ChangTeach
      },
      {
        path: '/personal',
        component: PernalMessage
      },
      {
        path: '/usermanage',
        component: UserManage
      },
      {
        path: '/coursemanage',
        component: CourseManage
      },
      {
        path: '/freeclassroom',
        component: FreeClassroom
      }
    ]
  },
  {
    path: '/home',
    component: Home,
    redirect: '/personalmain',
    children: [
      {
        path: '/personalmain',
        component: PersonalMain
      },
      {
        path: '/addcourse',
        component: Addcourse
      },
      {
        path: '/pernalmessage',
        component: PernalMessage
      },
      {
        path: '/numberselect',
        component: Numberselect
      },
      {
        path: '/addcourse2',
        component: AddCourse2
      },
      {
        path: '/classselect',
        component: ClassSelect
      }
    ]
  },
  {
    path: '/chang',
    component: Chang
  }
]
const router = new VueRouter({
  routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('user')
  if (!tokenStr) return next('/login')
  next()
})
export default router
